package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportProductVo;
import cn.itcast.vo.ExportResult;
import cn.itcast.vo.ExportVo;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/cargo/export")
public class ExportController extends BaseController {

	@Reference
	private ContractService contractService;

	@Reference
	private ExportService exportService;

	@Reference
	private ExportProductService exportProductService;

	/**
	 * 查询所有状态=1的购销合同
	 */
	@RequestMapping("/contractList")
	public String contractList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
		//1.创建example
		ContractExample example = new ContractExample();
		//2.创建criteria对象
		ContractExample.Criteria criteria = example.createCriteria();
		//3.向criteria对象中设置查询条件
		criteria.andCompanyIdEqualTo(getLoginCompanyId());
		//4.设置状态
		criteria.andStateEqualTo(1);
		PageInfo info = contractService.findAll(page, size, example);
		request.setAttribute("page",info);
		return "cargo/export/export-contractList";
	}

	/**
	 * 查询报运单列表
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
		ExportExample example = new ExportExample();
		ExportExample.Criteria criteria = example.createCriteria();
		criteria.andCompanyIdEqualTo(getLoginCompanyId());
		PageInfo info = exportService.findAll(example, page, size);
		request.setAttribute("page",info);
		return "cargo/export/export-list";
	}

	/**
	 * 进入新增报运单页面
	 *      参数：传递同名参数id
	 *             springmvc中：
	 *                  id数组
	 *                  id字符串，多个id之间以“，”隔开
	 */
	@RequestMapping("/toExport")
	public String toExport(String id){
		request.setAttribute("id",id);
		return "cargo/export/export-toExport";
	}

	/**
	 * 进入修改报运单页面
	 *  参数 ： 报运单id
	 */
	@RequestMapping("/toUpdate")
	public String toUpdate(String id){
		//1.根据id查询报运单
		Export export = exportService.findById(id);
		request.setAttribute("export",export);
		//2.根据报运单id，查询报运单下的所有商品 (exportId)
		ExportProductExample example = new ExportProductExample();
		ExportProductExample.Criteria criteria = example.createCriteria();
		criteria.andExportIdEqualTo(id);
		List<ExportProduct> eps = exportProductService.findAll(example);
		request.setAttribute("eps",eps);
		return "cargo/export/export-update";
	}


	/**
	 * 新增或修改
	 */
	@RequestMapping("/edit")
	public String edit(Export export) {

		export.setCompanyId(getLoginCompanyId());
		export.setCompanyName(getLoginCompanyName());

		if(StringUtils.isEmpty(export.getId())) {
			//保存
			exportService.save(export);
		}else{
			//更新
			exportService.update(export);
		}
		return "redirect:/cargo/export/list.do";
	}


	/**
	 * 电子保运
	 *      参数：报运单ID
	 */
	@RequestMapping("/exportE")
	public String exportE(String id) {
		//1.根据id查询报运单对象Export
		Export export = exportService.findById(id);
		//2.根据报运单ID查询报运单商品ExportProduct
		ExportProductExample example = new ExportProductExample();
		ExportProductExample.Criteria criteria = example.createCriteria();
		criteria.andExportIdEqualTo(id);
		List<ExportProduct> eps = exportProductService.findAll(example);
		//3.根据自己的报运单对象Export封装海关保运需要的ExportVo对象
		ExportVo evo = new ExportVo();
		BeanUtils.copyProperties(export,evo);
		evo.setExportId(export.getId());
		//4.根据自己的商品对象ExportProduct集合封装海关保运需要的ExportProductVo集合对象
		List<ExportProductVo> epvs = new ArrayList<>();
		for (ExportProduct ep : eps) {
			ExportProductVo epvo = new ExportProductVo();
			BeanUtils.copyProperties(ep,epvo);
			epvo.setExportProductId(ep.getId()); //商品id
			epvo.setEid(id);
			epvs.add(epvo);
		}
		//5.配置ExportVo对象和ExportProductVo集合对象的关系
		evo.setProducts(epvs);
		//6.通过工具类WebClient发送保数据
		WebClient wc = WebClient.create("http://localhost:9090/ws/export/user");
		wc.post(evo); //发送数据
		//7.通过工具类webClient查询保运结果
		WebClient wc1 = WebClient.create("http://localhost:9090/ws/export/user/"+id);
		ExportResult exportResult = wc1.get(ExportResult.class);
		//8.更新报运结果
		exportService.updateE(exportResult);
		return "redirect:/cargo/export/list.do";
	}
}
